package coint.btc.dao.auth.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties.Hibernate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import coint.btc.base.dao.BaseDAO;
import coint.btc.dao.auth.JueseCustom;
import coint.btc.dto.JueseDTO;
import coint.btc.pojo.Juese;
import coint.btc.vo.JueseVo;
import coint.btc.vo.Page;

@Repository
@SuppressWarnings("all")
public class JueseJpaImpl extends BaseDAO implements JueseCustom {

	@Override
	@Transactional(readOnly = true)
	public Set<String> findRoleNameByUserId(Integer id) {
		String sql = "select a.name from juese a left join yonghu b on a.id=b.juese_id where b.id=:ID";
		return new HashSet<>(
				getSession().createSQLQuery(sql.toString()).setParameter("ID", id, StandardBasicTypes.INTEGER).list());
	}
	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
	public Page<JueseVo> findJuesePageByParms(Page<JueseVo> page, JueseDTO parms) {
		String sql = "SELECT sql_calc_found_rows a.id,a.name as juese_name ,a.remark  FROM juese a  where 1=1";
		/*模糊查询角色*/
		if (StringUtils.isNotBlank(parms.getJuese_name())) {
			sql += " and a.name like '%" + parms.getJuese_name() + "%";
		}
	

		Query query = getSession().createSQLQuery(sql).addScalar("juese_name", StandardBasicTypes.STRING)
				.addScalar("remark", StandardBasicTypes.STRING).addScalar("id", StandardBasicTypes.INTEGER);
		List<JueseVo> list= query.setResultTransformer(Transformers.aliasToBean(JueseVo.class)).setFirstResult(page.getRows() * (page.getPage() - 1)).setMaxResults(page.getRows()).list();
		page.setResults(list);
		Object uniqueResult = getSession().createSQLQuery("select found_rows()").uniqueResult();
		page.setTotal(uniqueResult);
		return page;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Juese findJueseByid(Integer id) {
		
		String sql = " SELECT a.id,a.name,a.remark FROM juese a where  id = " +id;
		
		Juese role= (Juese) getSession().createSQLQuery(sql).addEntity(Juese.class).uniqueResult();
		
		return role;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int updateJuese(Juese juse) {
		
		try {
			getSession().merge(juse);
			return 1;
		} catch (Exception e) {
			return  0 ;
		}
		
		
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int deleteJueseByid(Integer id) {
		String hql="delete from Juese lib where lib.id=" + id;
		Query queryupdate=getSession().createQuery(hql);
		int ret=queryupdate.executeUpdate();
		
		return ret;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int addJuese(Juese juese) {
		getSession().save(juese);
		if(juese.getId()>0) {
		return 1;
		}
		return 0;
	}
	

	
	
	
	
	
	
	
}
